package com.sample.library.ext

import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ObservableList

typealias FragmentViewBindingInflater<VB> = (
    inflater: LayoutInflater, parent: ViewGroup?, attachToParent: Boolean,
) -> VB

typealias ActivityViewBindingInflater<VB> = (inflater: LayoutInflater) -> VB

typealias ProxyClickListener = (View) -> Unit

fun <T> ObservableList<T>.addOnListChangedCallback(consumer: (Int, Int, Boolean) -> Unit) {

    addOnListChangedCallback(object : ObservableList.OnListChangedCallback<ObservableList<T>>() {
        override fun onChanged(sender: ObservableList<T>?) {
        }

        override fun onItemRangeRemoved(sender: ObservableList<T>?, positionStart: Int, itemCount: Int) {
            sender?.let { consumer.invoke(itemCount, positionStart, true) }
        }

        override fun onItemRangeMoved(sender: ObservableList<T>?, fromPosition: Int, toPosition: Int, itemCount: Int) {
        }

        override fun onItemRangeInserted(sender: ObservableList<T>?, positionStart: Int, itemCount: Int) {
            sender?.let { consumer.invoke(itemCount, positionStart, false) }
        }

        override fun onItemRangeChanged(sender: ObservableList<T>?, positionStart: Int, itemCount: Int) {
        }
    })
}

fun String.removeEscapeCharacter(): String = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY).toString()
} else {
    Html.fromHtml(this).toString()
}
